const express = require('express')
const router = express.Router()

const fs = require("fs")
const jsonData = require("../data/shuziren")


let tempNum = 0;
// 内部错误
const error5001 = jsonData[13].resJson;
// 参数错误
const error5005 = jsonData[14].resJson;
// 01-开启对话
router.post('/chathub/session/begin', async (req, res) => {
    const params = req.body;
    let resData = jsonData[1].resJson;
    // 修改topic
    resData.body.topic = params.topic;
    jsonWrite(res, resData);
})
// 02-获取对话答案
router.post('/chathub/session/chating', async (req, res) => {
    const text = req.body.input;
    let resData = undefined;
    if (text === '结束演示' || text === '好的好的') {
        // 返回1001
        resData = jsonData[1].resJson;
        resData.body.topic = req.body.topic;
        jsonWrite(res, resData)
        return;
    }
    for (const i in jsonData) {
        const data = jsonData[i];
        if (data.keys.includes(text)) {
            resData = data.resJson;
            break;
        }
    }

    if (resData === undefined) {
        tempNum++;
        if (tempNum === 4) {
            // 返回退出
            resData = jsonData[0].resJson;
            // 清空计数器
            tempNum = 0;
        } else {
            resData = tempNum % 2 === 0 ? jsonData[12].resJson : jsonData[11].resJson;
        }
    } else {
        // 有答案清空计数器
        tempNum = 0;
    }
    // 修改topic
    if (resData.body && resData.body.topic) {
        resData.body.topic = req.body.topic;
    }
    jsonWrite(res, resData)
})
// 03-单个视频制作
router.post('/videomaker/v2/video/make', async (req, res) => {
    const params = req.body;
    let resData =
        {
            code: 0,
            msg: "",
            body: {
                need_time: 10,
                task_id: "306437b3-ef63-4c12-bf24-2f8e8a7c5a1d"
            }
        };
    // input_ssml
    // anchor_solution
    if (!params.anchor_solution) {
        // 校验参数，必填anchor_solution
        resData = error5005;
    } else {
        let dataId = undefined;
        // 根据文本查找视频id
        for (const i in jsonData) {
            const data = jsonData[i];
            if (data.answer.includes(params.input_ssml)) {
                dataId = data.id;
                break;
            }
        }
        if (dataId) {
            resData.body.task_id = dataId;
        } else {
            // 查不到，返回参数错误
            resData = error5001
        }
    }
    jsonWrite(res, resData);
})

// 04-下指视频
router.get('/videomaker/v2/video/get', async (req, res) => {
    const params = req.query;
    const task_id = params.task_id;
    const url = `http://82.157.33.142/video/${task_id}.mp4`
    let videoTime = "";
    // taskId是视频id
    for (let i in jsonData) {
        const data = jsonData[i];
        if (data.id === task_id){
            videoTime = data.videoTime
        }
    }
    // 根据task_id返回视频地址
    let resData = {
        "code": 0,
        "msg": "",
        "body": {
            "video_url": url,
            "subtitles_url": url,
            "total_time": videoTime,
            "progress": 100,
            "need_time": 20,
            "err_msg": ""
        }
    }
    jsonWrite(res, resData)
})


// 接口：解析数据
router.get('/paseData', async (req, res) => {
    let data = [];
    await fs.readFile("./data.txt", "utf8", function (err, dataStr) {
        console.log(err, "-err");
        console.log("dataStr-----", dataStr);
        // 如果读取成功，则err的值为null，dataStr会显示例1.txt的文本内容
        // 如果读取失败，err的值为错误对象，展示出错误信息，dataStr的值为undefined
        dataArr = dataStr.split("\r\n")
        dataArr.forEach(d1 => {
            dataArr2 = d1.split("	");
            let r = {
                id: dataArr2[0],
                answer: dataArr2[1],
                keys: dataArr2[2],
                resJson: JSON.parse(dataArr2[3])
            }
            console.log("r---------", r);
            data.push(r)
        });
        console.log("data---", data);
        jsonWrite(res, data)
    })
})

// 接口：增加信息sql,编辑修改信息sql1
router.post('/save', (req, res) => {
    let data = {msg: "succss"}
    jsonWrite(res, data)
})


const jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1', msg: '操作失败'
        })
    } else {
        res.json(
            ret
        )
    }
}

module.exports = router